package com.ysstech.activiti.config;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Properties;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ActivitiException;
import org.activiti.spring.SpringProcessEngineConfiguration;
@Slf4j
public class SpringProcessEngineConfigurationWithGauss extends SpringProcessEngineConfiguration {
    private static Properties databaseTypeMappings = getDefaultDatabaseTypeMapping2s();
    //增加dm数据库支持
    public static final String DATABASE_TYPE_DM = "oracle";
    public static Properties getDefaultDatabaseTypeMapping2s() {
        databaseTypeMappings = getDefaultDatabaseTypeMappings();
        //增加dm数据库支持  按照oracle处理
        databaseTypeMappings.setProperty("DM DBMS", DATABASE_TYPE_DM);
        return databaseTypeMappings;
    }

    @Override
    public void initDatabaseType() {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            DatabaseMetaData databaseMetaData = connection.getMetaData();
            String databaseProductName = databaseMetaData.getDatabaseProductName();
            log.debug("database product name: '{}'", databaseProductName);
            databaseType = databaseTypeMappings.getProperty(databaseProductName);
            if (databaseType == null) {
                throw new ActivitiException("couldn't deduct database type from database product name '" + databaseProductName + "'");
            }
            log.debug("using database type: {}", databaseType);

            // Special care for MSSQL, as it has a hard limit of 2000 params per statement (incl bulk statement).
            // Especially with executions, with 100 as default, this limit is passed.
            if (DATABASE_TYPE_MSSQL.equals(databaseType)) {
                maxNrOfStatementsInBulkInsert = DEFAULT_MAX_NR_OF_STATEMENTS_BULK_INSERT_SQL_SERVER;
            }

        } catch (SQLException e) {
            log.error("Exception while initializing Database connection", e);
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                log.error("Exception while closing the Database connection", e);
            }
        }
    }
}
